<?php
/**
 * Created by PhpStorm.
 * User: 盛茂国
 * Tel: 18394333958
 * Date: 2020/6/2
 * Time: 10:31
 */
namespace app\api\controller;

use app\admin\command\Api;

use function GuzzleHttp\Psr7\str;
use think\Db;
use think\Request;

class Worker extends Api
{
    protected $noNeedLogin = ['index','detail','discuss'];
    protected $noNeedRight = '*';

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $pageNum=($request->param('pageNum')) ? $request->param('pageNum') : 5;
        $page=($request->param('page')) ? $request->param('page','intval') :0;
        $list=Db::name('worker')->page($page,$pageNum)->order('workerid desc')->select();
        foreach ($list as $key=>$value){
            $list[$key]['image']=$request->domain().$value['image'];
        }

        $data['total']=db('worker')->count();
        $data['page']=$page;
        $data['pageNum']=$pageNum;
        $data['data']=$list;
        return json(['status'=>200,'msg'=>'获取作品列表成功','data'=>$data]);
    }

    public function detail(Request $request){
        $pageNum=($request->param('pageNum')) ? $request->param('pageNum') : 5;
        $page=($request->param('page')) ? $request->param('page','intval') :0;
        if($request->isPost()){
            $workerid=$request->param('workerid');
            $openid=$request->param('openid');
            $uid=db('user')->where('open_id','=',$openid)->value('id');
            if(!empty($workerid)){
                $worker=new \app\admin\model\Worker();
                $obj=$worker->with('workinfo')->with('comments')->where('worker.workerid','=',$workerid)->find();
                if($obj){
                    $list=$obj->toArray();
                    //print_r($list);
                    $list['image']=[$request->domain().$list['image']];

                    foreach ($list['comments'] as $key=>$value){
                        $strsrc=$value['imageData'];
                        $strsrc=trim($strsrc,'[');
                        $strsrc=trim($strsrc,']');
                        $strsrc=str_replace('\\','',$strsrc);
                        $imgarr=explode(',',$strsrc);
                        if(is_array($imgarr) && count($imgarr) > 1){//多图
                            foreach ($imgarr as $k=>$v){
                                $imgarr[$k]= $request->domain().str_replace('"','',$v);
                            }
                            $list['comments'][$key]['imageData']=$imgarr;
                        }else{//单图
                            //print_r(strlen($imgarr[0]));
                            if(strlen($imgarr[0])>8){
                                $list['comments'][$key]['imageData']= [$request->domain().str_replace('"','',$imgarr[0])];
                            }else{
                                $list['comments'][$key]['imageData']=[];
                            }

                        }


                        $list['comments'][$key]['addtime']=date('Y-m-d H:i:s',$value['addtime']);
                        $user=db('user')->find(['id'=>$value['uid']]);
                        $list['comments'][$key]['nickname']=$user['nickname'];
                        $list['comments'][$key]['avatar']=$user['avatar'];
                    }

                    //注入作品收藏状态
                    $where['uid']=$uid;
                    $where['workerid']=$workerid;
                    $collectstatus=db('workercollect')->where($where)->value('status');
                    $likestatus=db('workerlike')->where($where)->value('status');
                    if ($collectstatus){
                        $collectstatus='true';
                    }else{
                        $collectstatus='false';
                    }
                    $list['collectstatus']=$collectstatus;
                    if ($likestatus){
                        $likestatus='true';
                    }else{
                        $likestatus='false';
                    }
                    $list['likestatus']=$likestatus;

                    $list['workinfo']['content']=preg_replace('%src="/uploads%','src="'.$request->domain().'/uploads/', $list['workinfo']['content']);

                    $list['comments']=mypage($pageNum,$page,$list['comments'],1);

                    return json(['status'=>200,'msg'=>'请求成功','data'=>$list]);
                }else{
                    return json(['status'=>400,'msg'=>'暂时没有数据','data'=>[]]);
                }
            }else{
                return json(['status'=>400,'msg'=>'参数错误']);
            }
        }else{
            return json(['status'=>500,'msg'=>'非法请求']);
        }
    }



    /**
     * 作品点赞接口
     *
     * @return \think\Response
     */
    public function like(Request $request)
    {
        $openid=$request->param('openid');
        $status=$request->param('status');
        $workerid=$request->param('workerid');
        $uid=db('user')->where('open_id',$openid)->value('id');

        $workerlike=db('workerlike');
        $bool=$workerlike->where(['uid'=>$uid,'workerid'=>$workerid])->find();
        //$status=null;   //true 收藏  false 取消
        if($status==='false'){
            $status=1;
        }else if($status==='true'){
            $status=0;
        }

        if($bool){
            $is=$workerlike->where(['uid'=>$uid,'workerid'=>$workerid])->data(['status'=>$status,'createtime'=>time()])->update();
            if($status){
                db('worker')->where('workerid','=',$workerid)->setInc('favitor',1);
            }else{
                db('worker')->where('workerid','=',$workerid)->setDec('favitor',1);
            }
            if($is){
                $status ? $msg='赞成功' : $msg="取消赞";
                $data=$workerlike->where(['uid'=>$uid,'workerid'=>$workerid])->find();
                return json(['status'=>200,'msg'=>$msg,'data'=>$data]);
            }else{
                return json(['status'=>400,'msg'=>'入库失败']);
            }
        }else{
            $is=$workerlike->data(['uid'=>$uid,'status'=>$status,'workerid'=>$workerid,'createtime'=>time()])->insert();
            if($status){
                db('worker')->where('workerid','=',$workerid)->setInc('favitor',1);
            }else{
                db('worker')->where('workerid','=',$workerid)->setDec('favitor',1);
            }
            if($is){
                $status ? $msg='赞成功' : $msg="取消赞";
                $data=$workerlike->where(['uid'=>$uid,'workerid'=>$workerid])->find();
                return json(['status'=>200,'msg'=>$msg,'data'=>$data]);
            }else{
                return json(['status'=>400,'msg'=>'入库失败']);
            }
        }
    }

    /**
     * 作品收藏接口
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function collect(Request $request)
    {
        $openid=$request->param('openid');
        $status=$request->param('status');
        $workerid=$request->param('workerid');
        $uid=db('user')->where('open_id',$openid)->value('id');

        $workerlike=db('workercollect');
        $bool=$workerlike->where(['uid'=>$uid,'workerid'=>$workerid])->find();

        //$status=null;   //true 收藏  false 取消
        if($status==='false'){
            $status=1;
        }else if($status==='true'){
            $status=0;
        }
        if($bool){
            $is=$workerlike->where(['uid'=>$uid,'workerid'=>$workerid])->data(['status'=>$status,'createtime'=>time()])->update();
            /*if($status){
                db('worker')->where('workerid','=',$workerid)->setInc('favitor',1);
            }else{
                db('worker')->where('workerid','=',$workerid)->setDec('favitor',1);
            }*/
            $status ? $msg='收藏成功' : $msg="取消收藏";
            $data=$workerlike->where(['uid'=>$uid,'workerid'=>$workerid])->find();
            return json(['status'=>200,'msg'=>$msg,'data'=>$data]);
        }else{
            $is=$workerlike->data(['uid'=>$uid,'status'=>$status,'workerid'=>$workerid,'createtime'=>time()])->insert();
            /*if($status){
                db('worker')->where('workerid','=',$workerid)->setInc('favitor',1);
            }else{
                db('worker')->where('workerid','=',$workerid)->setDec('favitor',1);
            }*/
            if($is){
                $status ? $msg='收藏成功' : $msg="取消收藏";
                $data=$workerlike->where(['uid'=>$uid,'workerid'=>$workerid])->find();
                return json(['status'=>200,'msg'=>$msg,'data'=>$data]);
            }else{
                return json(['status'=>400,'msg'=>'入库失败']);
            }
        }
    }
}
